import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

public class UserProfile {
	
    private static String API_KEY = "xGN81IwCaUMozMrNruRKSADVnprDMsnX";

    public static String getApiUri(String uri) {
        return "http://www.plurk.com/API" + uri;
    }

/*    private int karma,gender,friends_count,fans_count,alerts_count,unread_count;
    private double uid,id;
    private String location,relationship,nick_name,display_name,timezone,full_name,privacy;
    private Date dateOfBirth;
    private boolean recruited,avatar,has_profile_image,has_read_permission;*/
    
    private JSONObject objRaw,objUserInfo;
    
    public UserProfile(){
    	
    }
    
	public UserProfile(String uName, String uPass) throws Exception{
    	HttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet(getApiUri("/Users/login?"+
                            "api_key=" + API_KEY + "&" +
                            "username=" + uName + "&" +
                            "password=" + uPass
                          ));
        
        ResponseHandler responseHandler = new BasicResponseHandler();
        //System.out.println(httpclient.execute(httpget, responseHandler));
        objRaw = new JSONObject(httpclient.execute(httpget, responseHandler).toString());
        objUserInfo = new JSONObject(objRaw.get("user_info").toString());
        httpclient.getConnectionManager().shutdown();
        
	}
	
	public float getKarma() throws Exception{
		return Float.parseFloat(objUserInfo.get("karma").toString());
	}
	
	public int getGender() throws Exception{
		return Integer.parseInt(objUserInfo.get("gender").toString());
	}
	
	public int getFriendsCount() throws Exception{
		return Integer.parseInt(objRaw.get("friends_count").toString());
	}
	
	public int getFansCount() throws Exception{
		return Integer.parseInt(objRaw.get("fans_count").toString());
	}
	
	public int getAlertsCount() throws Exception{
		return Integer.parseInt(objRaw.get("alerts_count").toString());
	}
	
	public int getUnreadCount() throws Exception{
		return Integer.parseInt(objRaw.get("unread_count").toString());
	}
	
	public int getRecruited() throws Exception{
		return Integer.parseInt(objUserInfo.get("recruited").toString());
	}
	
	public int getAvatar() throws Exception{
		return Integer.parseInt(objUserInfo.get("recruited").toString());
	}
	
	public boolean hasProfileImage() throws Exception{
		return Boolean.parseBoolean(objRaw.get("has_profile_image").toString());
	}
	
	public boolean hasReadPermission() throws Exception{
		return Boolean.parseBoolean(objRaw.get("has_read_permission").toString());
	}
	
	public double getUid() throws Exception{
		return Double.parseDouble(objUserInfo.get("uid").toString());
	}
	
	public double getId() throws Exception{
		return Double.parseDouble(objUserInfo.get("id").toString());
	}
	
	public String getLocation() throws Exception{
		return objUserInfo.get("location").toString();
	}
	
	public String getRelationship() throws Exception{
		return objUserInfo.get("relationship").toString();
	}
	
	public String getNickName() throws Exception{
		return objUserInfo.get("nick_name").toString();
	}
	
	public String getDisplayName() throws Exception{
		return objUserInfo.get("display_name").toString();
	}
	
	public String getTimeZone() throws Exception{
		return objUserInfo.get("timezone").toString();
	}
	
	public String getFullName() throws Exception{
		return objUserInfo.get("full_name").toString();
	}
	
	public String getPrivacy() throws Exception{
		return objRaw.get("privacy").toString();
	}
	
	public Date getBirth() throws Exception{
		DateFormat dateFormat = new SimpleDateFormat("E, d M y");
		return dateFormat.parse(objUserInfo.get("date_of_birth").toString());
	}	
	
}
